<title>商品管理</title>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="goods-goods-index-search_from">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">ID</label>
                    <div class="layui-input-inline">
                        <input type="text" name="id" placeholder="请输入ID" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">商品名称</label>
                    <div class="layui-input-inline">
                        <input type="text" name="title" placeholder="请输入商品名称" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">分类</label>
                    <div class="layui-input-inline">
                        <script type="text/html" template lay-url="{{ layui.setter.apiHost }}/goods/category/select_all" lay-done="layui.form.render();">
                            <select name="category_id">
                                <option value=""></option>
                                {{# layui.each(d.data, function(index, item){ }}
                                <option value="{{ item.id }}" {{# if(item.children){ }}disabled{{# } }}>{{ item.title }}</option>
                                {{# if(item.children){ }}
                                {{# layui.each(item.children, function(index1, item1){ }}
                                <option value="{{ item1.id }}" {{# if(item1.children){ }}disabled{{# } }}>--{{ item1.title }}</option>
                                {{# if(item1.children){ }}
                                {{# layui.each(item1.children, function(index2, item2){ }}
                                <option value="{{ item2.id }}" {{# if(item2.children){ }}disabled{{# } }}>----{{ item2.title }}</option>
                                {{# }); }}
                                {{# } }}
                                {{# }); }}
                                {{# } }}
                                {{# }); }}
                            </select>
                        </script>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">品牌</label>
                    <div class="layui-input-inline">
                        <script type="text/html" template lay-url="{{ layui.setter.apiHost }}/system/brand/select" lay-done="layui.form.render();">
                            <select name="brand_id">
                                <option value=""></option>
                                {{# layui.each(d.data, function(index, item){ }}
                                <option value="{{ index }}">{{ item }}</option>
                                {{# }); }}
                            </select>
                        </script>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">审核状态</label>
                    <div class="layui-input-inline">
                        <select name="status">
                            <option value=""></option>
                            <option value="0">锁定</option>
                            <option value="1">审核</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">上架状态</label>
                    <div class="layui-input-inline">
                        <select name="shelves_status">
                            <option value=""></option>
                            <option value="0">下架</option>
                            <option value="1">上架</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="goods-goods-index-search_button">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                </div>
            </div>
        </div>

        <script type="text/html" id="goods-goods-index-toolbar_tmp">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm" lay-event="goods-goods-index-add">添加</button>
                <button class="layui-btn layui-btn-sm" lay-event="goods-goods-index-status_on">审核</button>
                <button class="layui-btn layui-btn-sm" lay-event="goods-goods-index-status_off">锁定</button>
                <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="goods-goods-index-del">删除</button>
            </div>
        </script>
        <div class="layui-card-body">
            <table id="goods-goods-index" lay-filter="goods-goods-index"></table>
            <script type="text/html" id="goods-goods-index-img_tmp">
                {{ d.title }}
                {{# if(d.image != ''){ }}
                <a href="{{ d.image }}" target="_blank"><img src="{{ layui.common.image_resize(d.image, 30, 30) }}" width="30" height="30"></a>
                {{# } }}
                {{# if(d.is_rem == 1){ }}
                <span class="layui-badge">推荐</span>
                {{# } }}
            </script>
            <script type="text/html" id="goods-goods-index-shelves_status_tmp">
                {{# if(d.shelves_status == 1){ }}
                <span class="layui-badge layui-bg-green">上架</span>
                {{# } else { }}
                <span class="layui-badge">下架</span>
                {{# } }}
            </script>
            <script type="text/html" id="goods-goods-index-status_tmp">
                <input type="checkbox" value="{{d.id}}" lay-skin="switch" lay-text="正常|锁定" lay-filter="goods-goods-index-status_btn" {{# if(d.status== 1){ }}checked{{# } }}>
            </script>
            <script type="text/html" id="goods-goods-index-action_button">
                <a class="layui-btn layui-btn-xs" lay-event="goods-goods-index-edit">编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="goods-goods-index-del">删除</a>
            </script>
        </div>
    </div>
</div>
<script>
    layui.use(['common', 'table', 'admin'], function () {
        var $ = layui.jquery,
            common = layui.common,
            admin = layui.admin,
            view = layui.view,
            table = layui.table,
            form = layui.form,
            lay_id_prefix = 'goods-goods-index',//当前页id前缀
            edit_id_prefix = 'goods-goods-add',//添加编辑页id前缀
            edit_view_url = 'goods/goods/add',//添加编辑页模板地址
            api_action_url = layui.setter.apiHost + '/goods/goods';

        //表单搜索
        form.on('submit(' + lay_id_prefix + '-search_button)', function (data) {
            var field = data.field;
            field.page = 1;//默认第一页
            table.reload(lay_id_prefix, {
                where: field
            });
        });

        //表格操作
        table.render({
            elem: '#' + lay_id_prefix,
            url: api_action_url,
            toolbar: '#' + lay_id_prefix + '-toolbar_tmp',
            cols: [[
                {type: 'checkbox', fixed: 'left'},
                {field: 'id', title: 'ID', width: 80},
                {field: 'title', title: '商品名称', minWidth: 200, toolbar: '#' + lay_id_prefix + '-img_tmp'},
                {field: 'category', title: '分类', width: 100},
                {field: 'sku_code', title: '货号', width: 140},
                {field: 'market_price', title: '市场价', width: 100},
                {field: 'sell_price', title: '销售价', width: 100},
                {field: 'shelves_status', title: '上架', width: 80, templet: '#' + lay_id_prefix + '-shelves_status_tmp'},
                {field: 'status', title: '状态', width: 100, templet: '#' + lay_id_prefix + '-status_tmp'},
                {field: 'created_at', title: '创建时间', width: 170},
                {title: '操作', width: 150, align: 'center', fixed: 'right', toolbar: '#' + lay_id_prefix + '-action_button'}
            ]],
            page: true
        });

        //头部工具栏操作
        table.on('toolbar(' + lay_id_prefix + ')', function (obj) {
            var type = obj.event;
            switch (type) {
                case lay_id_prefix + '-add':
                    select_category();
                    break;
                case lay_id_prefix + '-status_on':
                    common.ajax(api_action_url + '/status', lay_id_prefix, {status: 1});
                    break;
                case lay_id_prefix + '-status_off':
                    common.ajax(api_action_url + '/status', lay_id_prefix, {status: 0});
                    break;
                case lay_id_prefix + '-shelves_status_on':
                    common.ajax(api_action_url + '/shelves_status', lay_id_prefix, {status: 1});
                    break;
                case lay_id_prefix + '-shelves_status_off':
                    common.ajax(api_action_url + '/shelves_status', lay_id_prefix, {status: 0});
                    break;
                case lay_id_prefix + '-rem_on':
                    common.ajax(api_action_url + '/rem', lay_id_prefix, {is_rem: 1});
                    break;
                case lay_id_prefix + '-rem_off':
                    common.ajax(api_action_url + '/rem', lay_id_prefix, {is_rem: 0});
                    break;
                case lay_id_prefix + '-del':
                    layer.confirm('确定删除吗', function (index) {
                        common.ajax(api_action_url + '/delete', lay_id_prefix);
                    });
                    break;
            }
        });

        //监听工具条操作按钮
        table.on('tool(' + lay_id_prefix + ')', function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case lay_id_prefix + '-edit':
                    common.open_window('编辑', api_action_url, edit_view_url, edit_id_prefix, {id: data.id, width: '100%'});
                    break;
                case lay_id_prefix + '-del':
                    layer.confirm('确定删除吗', function (index) {
                        if (common.ajax(api_action_url + '/delete', lay_id_prefix, {id: data.id}, false)) {
                            obj.del();
                            layer.close(index);
                        }
                    });
                    break;
            }
        });

        //监听锁定操作
        form.on('switch(' + lay_id_prefix + '-status_btn)', function (obj) {
            var send_data = {id: this.value, status: obj.elem.checked == true ? 1 : 0};
            common.ajax(api_action_url + '/status', lay_id_prefix, send_data, false);
        });

        //添加商品前选择分类
        function select_category() {
            admin.popup({
                title: '选择分类'
                , area: ['500px', '500px']
                , id: 'goods-select_category-add' + new Date().getTime()
                , success: function (layero, index) {
                    view(this.id).render('goods/goods/select_category').done(function () {
                        //监听提交
                        form.on('submit(goods-select_category-add-form-submit)', function (data) {
                            var field = data.field; //获取提交的字段
                            if (!field.category_id) {
                                layer.msg('请选择分类', {time: 1000});
                            } else {
                                layer.close(index);
                                common.open_window('添加商品', api_action_url, edit_view_url, edit_id_prefix, {width: '100%'}, {category_id: field.category_id,sku_code: Date.parse(new Date())});
                            }
                        });
                    });
                }
            });
        }

    });
</script>
